package vy.org.app.page.printlabel.report.schedule;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.vy.dal.dao.VyPrintLabelReportDO;

import vy.org.app.common.ActionCtx;

public class PrintLabelReportHelper {
	public static final SimpleDateFormat _DATE_FMT = new SimpleDateFormat("MM/dd/yyyy");
	public static final String DELIMITER = " AND ";

	public static VyPrintLabelReportDO fillReportDO(MntnPrintLabelReportScheduleForm form)throws Exception {
		VyPrintLabelReportDO dto = new VyPrintLabelReportDO();
		dto.setReportId(Integer.parseInt(form.getReportId()));
		dto.setReportName(form.getReportName());
		dto.setReportType(form.getReportType());
		dto.setScheduleFrequency(form.getScheduleFrequency());
		dto.setScheduleDay(form.getScheduleDay());
		dto.setScheduleHour(form.getScheduleHour());
		dto.setScheduleMin(form.getScheduleMin());
		dto.setScheduleMonth(form.getScheduleMonth());
		dto.setScheduleWeekday(form.getScheduleWeekday());
		dto.setExpiryDate(_DATE_FMT.parse(form.getExpiryDate()));
		dto.setIsAutorun(form.isAutorun());
		dto.setIsActive(form.isActive());
		dto.setCreatedDate(new Date());
		dto.setLastModifiedOn(new Date());
		dto.setLastModifiedBy(ActionCtx.ctx().getUser().getId());
		String criteria = "";
		if(form.getPeriodMonth() != -1)
			criteria = "PATRIKA_ID=" + form.getPatrikaId()+DELIMITER;
		if(form.getPeriodMonth() != -1)
			criteria += "PERIOD_MONTH="+form.getPeriodMonth() +DELIMITER;
		criteria += "COUNTRY='"+ ("".equals(form.getCountry())?"%":form.getCountry()) +"'"+DELIMITER;
		criteria += "STATE='"+ ("".equals(form.getStateOrRegion())?"%":form.getStateOrRegion()) + "'" +DELIMITER;
		criteria += "CITY='"+ ("".equals(form.getCity())?"%":form.getCity()) + "'" +DELIMITER;
		if("N".equals(form.getQuantity())){
			criteria += "QUANTITY=N" +DELIMITER;
		}else if("1".equals(form.getQuantity())){
			criteria += "QUANTITY=1" +DELIMITER;
		}
		//if(form.getPeriodMonth() > -1)
		//	criteria += "WEEK_OFFSET_INDEX="+("".equals(form.getWeekOfsetIndex())?"'%'":form.getWeekOfsetIndex()) +DELIMITER;
		criteria += " 1=1 ";
		dto.setCriteria(criteria);
		
		return dto;
	}

	public static MntnPrintLabelReportScheduleForm fillReportForm(VyPrintLabelReportDO dto)throws Exception {
		MntnPrintLabelReportScheduleForm form = new MntnPrintLabelReportScheduleForm();
		form.setReportId(dto.getReportId().toString());
		form.setReportName(dto.getReportName());
		form.setReportType(dto.getReportType());
		form.setScheduleFrequency(dto.getScheduleFrequency().toString());
		form.setScheduleDay(dto.getScheduleDay());
		form.setScheduleHour(dto.getScheduleHour());
		form.setScheduleMin(dto.getScheduleMin());
		form.setScheduleMonth(dto.getScheduleMonth());
		form.setScheduleWeekday(dto.getScheduleWeekday());
		form.setExpiryDate(_DATE_FMT.format(dto.getExpiryDate()));
		
		if(dto.getIsAutorun() !=null)
			form.setAutorun(dto.getIsAutorun());
		if(dto.getIsActive())
			form.setActive(dto.getIsActive());
		String[] criterias = dto.getCriteria().split(DELIMITER);
		for(String term : criterias){
			String[] val = term.split("=");
			if("PATRIKA_ID".equals(val[0]))
				form.setPatrikaId(Integer.parseInt(val[1]));
			if("ceil(datediff(end_date, start_date)/30)".equals(val[1]))
				form.setPeriodMonth(Integer.parseInt(val[1]));
			if("COUNTRY".equals(val[0]))
				form.setCountry(val[1].replaceAll("'", ""));
			if("STATE".equals(val[0]))
				form.setStateOrRegion(val[1].replaceAll("'", ""));
			if("CITY".equals(val[0]))
				form.setCity(val[1].replaceAll("'", ""));
			if("QUANTITY".equals(val[0]))
				form.setQuantity(val[1]);
			if("PERIOD_MONTH".equals(val[0]) && !"".equals(val[1]) && !"%".equals(val[1]))
				form.setPeriodMonth(Integer.parseInt(val[1]));
			if("WEEK_OFFSET_INDEX".equals(val[0]))
				form.setWeekOfsetIndex(val[1]);
		}
		return form;
	}

	public static MntnPrintLabelReportScheduleForm fillReportForm(VyPrintLabelReportDO dto,
			MntnPrintLabelReportScheduleForm form)throws Exception {
		form.setReportId(dto.getReportId().toString());
		form.setReportName(dto.getReportName());
		form.setReportType(dto.getReportType());
		form.setScheduleFrequency(dto.getScheduleFrequency().toString());
		form.setScheduleDay(dto.getScheduleDay());
		form.setScheduleHour(dto.getScheduleHour());
		form.setScheduleMin(dto.getScheduleMin());
		form.setScheduleMonth(dto.getScheduleMonth());
		form.setScheduleWeekday(dto.getScheduleWeekday());
		form.setExpiryDate(_DATE_FMT.format(dto.getExpiryDate()));
		
		if(dto.getIsAutorun() !=null)
			form.setAutorun(dto.getIsAutorun());
		if(dto.getIsActive())
			form.setActive(dto.getIsActive());
		String[] criterias = dto.getCriteria().split(DELIMITER);
		for(String term : criterias){
			String[] val = term.split("=");
			if("PATRIKA_ID".equals(val[0]))
				form.setPatrikaId(Integer.parseInt(val[1]));
			if("ceil(datediff(end_date, start_date)/30)".equals(val[0]))
				form.setPeriodMonth(Integer.parseInt(val[1]));
			if("COUNTRY".equals(val[0]))
				form.setCountry(val[1].replaceAll("'", ""));
			if("STATE".equals(val[0]))
				form.setStateOrRegion(val[1].replaceAll("'", ""));
			if("CITY".equals(val[0]))
				form.setCity(val[1].replaceAll("'", ""));
			if("QUANTITY".equals(val[0]))
				form.setQuantity(val[1]);
			if("PERIOD_MONTH".equals(val[0]) && !"".equals(val[1]) && !"%".equals(val[1]))
				form.setPeriodMonth(Integer.parseInt(val[1]));
			if("WEEK_OFFSET_INDEX".equals(val[0]))
				form.setWeekOfsetIndex(val[1]);
		}
		return form;
	}
}
